package com.example.zzh.controller;


import com.example.zzh.entity.ZzhOrganization;
import com.example.zzh.entity.ZzhProductCategory;
import com.example.zzh.entity.ZzhStaff;
import com.example.zzh.service.ZzhFunctionService;
import com.example.zzh.service.ZzhOrganizationService;
import com.example.zzh.utils.Result;
import com.example.zzh.utils.ResultGenerator;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.*;

/**
 * <p>
 * 机构表 前端控制器
 * </p>
 *
 * @author zzh
 * @since 2023-08-06
 */
@RestController
@RequestMapping("/zzh-organization")
public class ZzhOrganizationController {
    @Autowired
    ZzhOrganizationService zzhOrganizationService;

    @GetMapping("/list")
    @ApiOperation(value = "查询所有部门(查询)", notes = "查询所有部门信息")
    public Result list(){
        List<ZzhOrganization> list=zzhOrganizationService.lambdaQuery().eq(ZzhOrganization::getDeleteFlag,"1").list();
        List<Map<String, Object>> treeData = new ArrayList<>();
        Map<Long, Map<String, Object>> map = new HashMap<>();
        for(int i=0;i<list.size();i++){
            ZzhOrganization arrayList=list.get(i);
            Map<String,Object> node=new HashMap<>();
            node.put("id",arrayList.getId());
            node.put("label",arrayList.getOrgAbr());
            // 将节点存储到map中，以便后续构建树形结构
            map.put(arrayList.getId(), node);
            if(arrayList.getParentId() != null){
                Map<String, Object> parent = map.get(arrayList.getParentId());
                if (parent != null) {
                    List<Map<String, Object>> children = (List<Map<String, Object>>) parent.get("children");
                    if (children == null) {
                        children = new ArrayList<>();
                        parent.put("children", children);
                    }
                    children.add(node);
                }
            } else {
                treeData.add(node); // 如果节点没有父节点，则将其作为根节点添加到treeData中
            }
        }
        return ResultGenerator.genSuccessResult(treeData);
    }

    @PostMapping("/add")
    @ApiOperation(value = "新增部门", notes = "新增部门信息")
    public Result add(@RequestBody ZzhOrganization zzhOrganization){
        zzhOrganization.setDeleteFlag("1");
        if(zzhOrganizationService.save(zzhOrganization)){
            return ResultGenerator.genSuccessResult();
        }else {
            return ResultGenerator.genErrorResult(404,"新增不成功");
        }
    }

    @PutMapping("/update")
    @ApiOperation(value = "修改部门", notes = "修改部门信息")
    public Result mod(@RequestBody ZzhOrganization zzhOrganization){
        if(zzhOrganizationService.updateById(zzhOrganization)){
            return ResultGenerator.genSuccessResult();
        }else {
            return ResultGenerator.genErrorResult(404,"修改不成功");
        }
    }

    @DeleteMapping("/delete")
    @ApiOperation(value = "删除部门", notes = "删除部门信息")
    public Result delete(@RequestParam int id){
        ZzhOrganization zzhOrganization=zzhOrganizationService.getRoleById(id);
        if(!Objects.isNull(zzhOrganization)){
            zzhOrganization.setDeleteFlag("0");
            if(zzhOrganizationService.updateById(zzhOrganization)) {
                return ResultGenerator.genSuccessResult();
            }
            return ResultGenerator.genErrorResult(404,"删除不成功");
        }else {
            return ResultGenerator.genErrorResult(404,"删除不成功");
        }
//        if(zzhOrganizationService.removeById(id)){
//            return ResultGenerator.genSuccessResult();
//        }else {
//            return ResultGenerator.genErrorResult(404,"删除不成功");
//        }
    }

    @PostMapping("/findeByOrgNo")
    @ApiOperation(value = "模糊查询部门id", notes = "模糊查询")
    public Result findeByOrgNo(@RequestParam String OrgNo){
        List list=zzhOrganizationService.lambdaQuery().eq(ZzhOrganization::getDeleteFlag,"1").eq(ZzhOrganization::getOrgNo,OrgNo).list();
        return list.size()>0?ResultGenerator.genSuccessResult(list.get(0)):ResultGenerator.genFailResult("不重复");
    }

    @PostMapping("/findeById")
    @ApiOperation(value = "id查询单个信息", notes = "根据id查询单个部门信息")
    public Result findeById(@RequestParam Long id){
        ZzhOrganization zzhOrganization=zzhOrganizationService.lambdaQuery().eq(ZzhOrganization::getId,id).list().get(0);
        System.out.println("zzhOrganization"+zzhOrganization.toString());
        if(zzhOrganization !=null){
            return ResultGenerator.genSuccessResult(zzhOrganization);
        }else {
            return ResultGenerator.genErrorResult(404,"删除不成功");
        }
    }
}
